#!/bin/bash
set -e

PROG="nogotofail-mitm"
PYTHON=`which python2.7`
PYMITM_DIR="/opt/nogotofail"
CONFIG_FILE="/etc/nogotofail/mitm.conf"
PID_PATH="/var/run/"
start() {
    start-stop-daemon -d $PYMITM_DIR -b -m --pidfile $PID_PATH/$PROG.pid  --startas $PYTHON -S -- -m nogotofail.mitm -c $CONFIG_FILE
    echo "$PROG started"
}

stop() {
    start-stop-daemon --pidfile $PID_PATH/$PROG.pid -K --retry 10
    echo "$PROG stopped"
}

## Check to see if we are running as root first.
## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html
if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

case "$1" in
    start)
        start
        exit 0
    ;;
    stop)
        stop
        exit 0
    ;;
    reload|restart|force-reload)
        stop
        start
        exit 0
    ;;
    **)
        echo "Usage: $0 {start|stop|reload}" 1>&2
        exit 1
    ;;
esac
